package com.example.zxq_server.whbgy.fragpack;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.zxq_server.whbgy.R;
import com.example.zxq_server.whbgy.adpterpack.GroupAdapter;
import com.example.zxq_server.whbgy.adpterpack.adptermodel.GroupModel;
import com.example.zxq_server.whbgy.adpterpack.adptermodel.PhoneModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


/**
 * Created by zxq_Server on 2017/9/9.
 */
public class PhoneFragment extends Fragment
{
    static String fileKey="PhoneKey";
    public  static  PhoneFragment newInstance(String phoneFile)
    {
        PhoneFragment phoneFragment=new PhoneFragment();
        Bundle bundle=new Bundle();
        bundle.putString(fileKey,phoneFile);
        phoneFragment.setArguments(bundle);
        return phoneFragment;
    }
    String localExcel="";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        localExcel =  getArguments().getString(fileKey);
        return inflater.inflate(R.layout.frag_phone,null);
    }
    GroupAdapter groupAdapter=null;
    RecyclerView myRecyle;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myRecyle= view.findViewById(R.id.myRecyle);

        android.util.Log.e("isEmpty",localExcel.isEmpty()+"");
        if(localExcel.isEmpty())
        {
            /*读取assets文档数据*/
            try {
                InputStream inputStream= getResources().getAssets().open("trl.xls");
                groupAdapter=new GroupAdapter(readExcel(inputStream));
            } catch (IOException e) {
                android.util.Log.e("isEmpty",e.toString()+"");
                e.printStackTrace();
            }
        }
        else
        {
            /*读取本地文档数据*/
          File local=  new File(localExcel);
            try {
                InputStream inputStream=new FileInputStream(local);
                groupAdapter=new GroupAdapter(readExcel(inputStream));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
      myRecyle.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
      myRecyle.setAdapter(groupAdapter);

    }






    private List<MultiItemEntity> readExcel(InputStream inputStream) {
        List<MultiItemEntity> modelList=new ArrayList<>();
        try {
            Workbook workbook =  Workbook.getWorkbook(inputStream);
            inputStream.close();
            //获取第一个工作表的对象
            Sheet sheet = workbook.getSheet(0);
            //获取第一列第一行的的单元格
            int cloum=0;
            GroupModel  tempGroup = null;
            do {

                String title = sheet.getCell(0, cloum).getContents();
                String phoneName=sheet.getCell(1, cloum).getContents();
                String phoneNumber=sheet.getCell(2,cloum).getContents();
                android.util.Log.e("title",title);
                android.util.Log.e("phoneName",phoneName);
                android.util.Log.e("phoneNumber",phoneNumber);
                if(!title.isEmpty())
                {
                    tempGroup=new GroupModel();
                    tempGroup.setGroupName(title);
                    modelList.add(tempGroup);
                }

                    PhoneModel phoneModel=new PhoneModel();
                    phoneModel.setPhoneName(phoneName);
                    phoneModel.setPhoneNumber(phoneNumber);
                    tempGroup.addSubItem(phoneModel);
                    cloum++;



            }while (!sheet.getCell(1,cloum).getContents().isEmpty());

            workbook.close();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }finally {
            return  modelList;
        }

    }

}
